﻿using System;
using System.Media;

static class UserInterface
{
    private static void DrawWelcomeScreen()
    {
        ConsoleTweaks.RemoveScrollBars();
        Console.BackgroundColor = ConsoleColor.White;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.DarkRed;
        ConsoleTweaks.CenterTextOnRow("Donald Duck Entertainment presents:", Console.WindowHeight / 2 - 2);       
        ConsoleTweaks.CenterTextOnRow("***********************", Console.WindowHeight / 2);
        ConsoleTweaks.CenterTextOnRow("*       THE GAME      *", Console.WindowHeight / 2 + 1);
        ConsoleTweaks.CenterTextOnRow("***********************", Console.WindowHeight / 2 + 2);
    }

    private static void DrawMainMenu(int highlighted)
    {
        ConsoleTweaks.RemoveScrollBars();
        Console.BackgroundColor = ConsoleColor.White;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.DarkRed;
        ConsoleTweaks.CenterTextOnRow("***********************", 1);
        ConsoleTweaks.CenterTextOnRow("*       THE GAME      *", 2);
        ConsoleTweaks.CenterTextOnRow("***********************", 3);
        ConsoleTweaks.CenterTextOnRow("Choose a game", 5);

        switch (highlighted)
        {
            case 1:
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 4);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 2);
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2);
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 2);
                ConsoleTweaks.CenterTextOnRow("Tournament", Console.WindowHeight / 2 + 4);
                ConsoleTweaks.CenterTextOnRow("Help", Console.WindowHeight / 2 + 6);
                break;
            case 2:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 4);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 2);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2);
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 2);
                ConsoleTweaks.CenterTextOnRow("Tournament", Console.WindowHeight / 2 + 4);
                ConsoleTweaks.CenterTextOnRow("Help", Console.WindowHeight / 2 + 6);
                break;
            case 3:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 4);
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 2);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 2);
                ConsoleTweaks.CenterTextOnRow("Tournament", Console.WindowHeight / 2 + 4);
                ConsoleTweaks.CenterTextOnRow("Help", Console.WindowHeight / 2 + 6);
                break;
            case 4:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 4);
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 2);
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 2);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;                
                ConsoleTweaks.CenterTextOnRow("Tournament", Console.WindowHeight / 2 + 4);
                ConsoleTweaks.CenterTextOnRow("Help", Console.WindowHeight / 2 + 6);
                break;
            case 5:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 4);
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 2);
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2);
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 2); 
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;                             
                ConsoleTweaks.CenterTextOnRow("Tournament", Console.WindowHeight / 2 + 4);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Help", Console.WindowHeight / 2 + 6);
                break;
            case 6:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 4);
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 2);
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2);
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 2);
                ConsoleTweaks.CenterTextOnRow("Tournament", Console.WindowHeight / 2 + 4);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;                
                ConsoleTweaks.CenterTextOnRow("Help", Console.WindowHeight / 2 + 6);
                break;
            default:
                break;
        }
    }

    private static void DrawHelpMenu(int highlighted)
    {
        ConsoleTweaks.RemoveScrollBars();
        Console.BackgroundColor = ConsoleColor.White;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.DarkRed;
        ConsoleTweaks.CenterTextOnRow("***********************", 1);
        ConsoleTweaks.CenterTextOnRow("*         HELP        *", 2);
        ConsoleTweaks.CenterTextOnRow("***********************", 3);

        switch (highlighted)
        {
            case 1:
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 3);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 1);
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2 + 1);
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 3);
                break;
            case 2:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 3);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 1);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;               
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2 + 1);
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 3);
                break;
            case 3:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 3);
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 1);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;                
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2 + 1);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;                               
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 3);
                break;
            case 4:
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.DarkRed;
                ConsoleTweaks.CenterTextOnRow("Rock Paper Scissors Lizard Spock", Console.WindowHeight / 2 - 3);
                ConsoleTweaks.CenterTextOnRow("Reflex Assessment", Console.WindowHeight / 2 - 1);
                ConsoleTweaks.CenterTextOnRow("Matrix Calculator", Console.WindowHeight / 2 + 1);
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.ForegroundColor = ConsoleColor.White;                
                ConsoleTweaks.CenterTextOnRow("Pairs Game", Console.WindowHeight / 2 + 3);                      
                break;
            default:
                break;
        }
    }

    private static int SelectDifficulty()
    {
        int difficulty;
        bool success;

        do
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Clear();
            ConsoleTweaks.CenterTextOnRow("Enter difficulty level: [1-9]", Console.WindowHeight / 2 - 1);
            Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
            success = int.TryParse(Console.ReadLine(), out difficulty);
        }
        while (!success || (difficulty < 1 || difficulty > 9));

        return difficulty;
    }

    public static void EnterGame(out int gameChoice, out int difficulty)
    {
        int helpChoice = 1;
        gameChoice = 1;
        DrawWelcomeScreen();
        Console.ReadKey();
        DrawMainMenu(gameChoice);
        ConsoleKeyInfo key = Console.ReadKey();

        while (key.Key != ConsoleKey.Enter)
        {
            switch (key.Key)
            {
                case ConsoleKey.UpArrow:
                    if (gameChoice == 1)
                    {
                        gameChoice = 6;
                    }
                    else
                    {
                        gameChoice--;
                    }
                    break;
                case ConsoleKey.DownArrow:
                    if (gameChoice == 6)
                    {
                        gameChoice = 1;
                    }
                    else
                    {
                        gameChoice++;
                    }
                    break;
                default:
                    SystemSounds.Beep.Play();
                    break;
            }
            DrawMainMenu(gameChoice);
            key = Console.ReadKey();

            if (key.Key == ConsoleKey.Enter && gameChoice == 6)
            {
                DrawHelpMenu(helpChoice);
                key = Console.ReadKey();

                while (key.Key != ConsoleKey.Enter && key.Key != ConsoleKey.Backspace)
                {
                    switch (key.Key)
                    {
                        case ConsoleKey.UpArrow:
                            if (helpChoice == 1)
                            {
                                helpChoice = 4;
                            }
                            else
                            {
                                helpChoice--;
                            }
                            break;
                        case ConsoleKey.DownArrow:
                            if (helpChoice == 4)
                            {
                                helpChoice = 1;
                            }
                            else
                            {
                                helpChoice++;
                            }
                            break;
                        default:
                            SystemSounds.Beep.Play();
                            break;
                    }
                    DrawHelpMenu(helpChoice);
                    key = Console.ReadKey();
                }

                if (key.Key == ConsoleKey.Enter)
                {
                    switch (helpChoice)
                    {
                        case 1:
                            Help.ExplainRockPaperLizard();
                            Console.ReadKey();
                            break;
                        case 2:
                            Help.ExplainReflexAssessment();
                            Console.ReadKey();
                            break;
                        case 3:
                            Help.ExplainMatrixCalculator();
                            Console.ReadKey();
                            break;
                        case 4:
                            Help.ExplainPairsGame();
                            Console.ReadKey();
                            break;
                        default:
                            break;
                    }
                }

                gameChoice = 1;
                key = new ConsoleKeyInfo('d', ConsoleKey.UpArrow, false, false, false);
            }
           
        }
        if (gameChoice != 5)
        {
            difficulty = SelectDifficulty();
        }
        else
        {
            difficulty = 9;
        }
    }
}